//
//  EnterViewController.m
//  NewChat
//
//  Created by songKX on 16/3/3.
//  Copyright © 2016年 蓝鸥. All rights reserved.
//

#import "EnterViewController.h"
#import <EaseMob.h>
#import <AVOSCloud/AVOSCloud.h>

@interface EnterViewController ()

@property (weak, nonatomic) IBOutlet UITextField *userName;
@property (weak, nonatomic) IBOutlet UITextField *userPwd;

@end

@implementation EnterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (self.userNameText.length!=0&&self.userPwdText.length!=0) {
        
        self.userName.text = self.userNameText;
        
        self.userPwd.text = self.userPwdText;
        
        
    }
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//登录操作

- (IBAction)loginAction:(UIButton *)sender {
    
    NSLog(@"开始登录");
    
    
    //环信登录
    
    
    [AVUser logInWithUsernameInBackground:self.userName.text password:self.userPwd.text block:^(AVUser *user, NSError *error) {
        if (user != nil) {
            
            NSLog(@"Leancloud登录成功");
            
            //实现环信登录
            
            [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:self.userName.text password:self.userPwd.text completion:^(NSDictionary *loginInfo, EMError *error) {
                
                
                if (!error && loginInfo) {
                    NSLog(@"环信登陆成功");
                    
                    //设置自动登录
                    [[EaseMob sharedInstance].chatManager setIsAutoLoginEnabled:YES];
                    //实现跳转
                    [self dismissViewControllerAnimated:YES completion:^{
                        
                    }];
                    
                }else{
                    NSLog(@"环信登陆失败");
                    //LeadCloud登出
                    [AVUser logOut];
                    
                }
            } onQueue:dispatch_get_main_queue()];
            
        } else {
            
            NSLog(@"Leancloud登录失败");
        }
    }];

    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
